package com.lanou3g.k12.service.impl;


import com.lanou3g.k12.entity.Article;
import com.lanou3g.k12.entity.ArticleContent;
import com.lanou3g.k12.mapper.ArticleContentMapper;
import com.lanou3g.k12.mapper.ArticleMapper;
import com.lanou3g.k12.service.ArticleService;
import com.lanou3g.k12.utils.GetImgUrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
// 资讯接口
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArticleService articleService;


    @Autowired
    ArticleContentMapper articleContentMapper;





    // 插入Article基本信息
    @Override
    public void addArticle(Long cid, String content, Integer status, String title,Integer sort) {


        // 获取content中的img地址
        String cover = GetImgUrlUtils.getUrl(content);
        System.out.println(cover);

        Article article = new Article();

        article.setCid(cid);
        article.setStatus(status);
        article.setTitle(title);
        article.setCover(cover);
        article.setSort(sort);

        Date date =new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);

        articleMapper.addArticle(article);
        articleMapper.updateSort(sort);

        Long id = article.getId();


        ArticleContent articleContent = new ArticleContent();
        articleContent.setId(id);
        articleContent.setContent(content);

        articleContentMapper.addArticleContent(articleContent);

    }

    // 插入Article文章内容
    @Override
    public void addArticleContent(ArticleContent articleContent) {

        articleContentMapper.addArticleContent(articleContent);

    }


    // 根据id查询到资讯
    @Override
    public Article findArticleById(Long id) {

       Article article = articleMapper.findArticleById(id);
        System.out.println(article);

       return article;
    }

    // 根据id更新资讯
    @Override
    public void updateArticleById(Long id, Long cid, Integer status, String title, String content, Integer sort) {
        Article article = articleService.findArticleById(id);

        if (sort!=null){
            article.setSort(sort);
            articleMapper.updateSort(sort);
        }else {
            article.setSort(article.getSort());
            sort = article.getSort();
        }

        Date date =new Date();
        article.setCid(cid);
        article.setStatus(status);
        article.setTitle(title);
        article.setUpdateTime(date);


        ArticleContent articleContent = articleContentMapper.findArticleContentById(id);
        String content1 = articleContent.getContent();
        if (content != null){
            articleContent.setContent(content);
            String cover = GetImgUrlUtils.getUrl(content);
            article.setCover(cover);
        }else {
            articleContent.setContent(content1);
            String cover = GetImgUrlUtils.getUrl(content1);
            article.setCover(cover);
        }

        articleMapper.setSort(id,sort);

        articleMapper.updateArticleById(article);

        articleContentMapper.updateArticleContent(articleContent);


    }


    //  根据id删除
    @Override
    public void deleteArticleById(Long id) {
        articleContentMapper.deleteArticleContentById(id);
        articleMapper.deleteArticleById(id);

    }

    // 查找所有并分页
    @Override
    public List<Article> findAll(Integer page, Integer size) {

        List<Article> articles = articleMapper.findAll();

        return articles;
    }

    // keyword查找并分页
    @Override
    public List<Article> findByKeyword(Integer page, Integer size, String keyword) {


        List<Article> articles =  articleMapper.findByKeyword('%' + keyword + '%');

        return articles;
    }


    // 根据id批量删除
    @Override
    public void deleteArticleByIds(Integer[] ids) {
        articleMapper.deleteArticleByIds(ids);
        articleContentMapper.deleteArticleContentByIds(ids);
    }




    @Override
    public Article findByIdForBackend(Long id) {
        Article article = articleMapper.findByIdForBackend(id);
        System.out.println(article);

        return article;

    }








    @Override
    public List<Article> findByKeywordForBackend(Integer page, Integer size, String keyword) {

        List<Article> articles =  articleMapper.findByKeywordForBackend('%' + keyword + '%');

        return articles;
    }

    @Override
    public List<Article> findAllForBackend(Integer page, Integer size) {
        List<Article> articles = articleMapper.findAllForBackend();

        return articles;
    }

}
